import java.util.Arrays;

public class QuikSortTest {

    public static void main(String[] args) {
        quickSort();
    }

    /**
     *
     */
    public static void quickSort() {
     
       int k = 3;
        int[] data = new int[]{-4,2,10,9, 8,7,6};
        System.out.println("before:"+Arrays.toString(data));
        partition(data,0,data.length-1);
        System.out.println("after :"+ Arrays.toString(data));
        if (k < data.length) {
            System.out.println("The " + k +" little number is: " + data[k-1]);
        }
    }

    
    private static void partition(int[] data,int startIndex,int pivotIndex) {
        if (data.length == 1) {
            return;
        }
        int pivot = data[pivotIndex];
        int location = startIndex;
        for (int index=startIndex;index < pivotIndex;index++) {
            if (data[index] < pivot) {
                int temp = data[location];
                data[location] = data[index];
                data[index] = temp;
                location++;
            }
        }
        int temp = data[location];
        data[location] = data[pivotIndex];
        data[pivotIndex] = temp;
        if (location-1 > startIndex) {
            partition(data,startIndex,location-1);
        }
        if (pivotIndex > location+1) {
            partition(data,location+1,pivotIndex);
        }
    }
}
